package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.dao.SysAreaDao;
import com.gusha.dao.SysMenuDao;
import com.gusha.dao.UserDao;
import com.gusha.model.SysArea;
import com.gusha.model.SysMenu;
import com.gusha.model.User;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;


/**
 * @Description: 不拦截的方法控制器
 * @Athor: 龙爷
 * @Company:
 * @Date: 2018-05-21 11:24:18
 */
@CrossOrigin(origins="*",maxAge=3600)
@Controller
@RequestMapping(value = "/common")
public class CommonController {
	
	@Autowired
	private SysMenuDao sysMenuDao;

	@Autowired
	private SysAreaDao sysAreaDao;
	@Autowired
	private UserDao userDao;
	
	/**
	 * @Description: 获取父菜单
	 * @Athor: 龙爷
	 * @Company:
	 * @Date: 2018-05-21 10:56:01
	 */
	
	@PostMapping("getParentMenuList")
	@ResponseBody
	public JsonResult getParentMenuList(Model model, HttpServletRequest request) {
		System.out.println("ok");
		JsonResult jsonResult = new JsonResult(false);
		String menuLevel = WebValueUtil.getWebValue(request, "menuLevel");
		Integer layerIndex = Integer.parseInt(menuLevel) - 1;
		List<SysMenu> list = sysMenuDao.findAllByMenuLevelOrderBySortDesc(layerIndex);
		
		jsonResult.setFlag(true);
		jsonResult.setObj(list);
		return jsonResult;
	}
	
	/**
	 * @Description: 获取省市区
	 * @Athor: 龙爷
	 * @Company:
	 * @Date: 2018-05-21 10:56:01
	 */
	@PostMapping("getAreaList")
	@ResponseBody
	public JsonResult getAreaList(Model model, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		String pCode = WebValueUtil.getWebValue(request, "pCode");
		List<SysArea> list = sysAreaDao.findAllByPCode(pCode);
		jsonResult.setFlag(true);
		jsonResult.setObj(list);
		return jsonResult;
	}
	
	/**
	 * @Description: 根据代理商获取营业部
	 * @Athor: 龙爷
	 * @Company:
	 * @Date: 2018-05-21 10:56:01
	 */
	@PostMapping("getYYBList")
	@ResponseBody
	public JsonResult getYYBList(Model model, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		String pId = WebValueUtil.getWebValue(request, "pId");
		List<User> list = userDao.findAllByUserTypeAndBelongId(4,new BigInteger(pId));
		jsonResult.setFlag(true);
		jsonResult.setObj(list);
		return jsonResult;
	}
	
	/**
	 * @Description: 根据业务员ID获取业务员编号
	 * @Athor: 龙爷
	 * @Company:
	 * @Date: 2018-05-21 10:56:01
	 */
	@PostMapping("getYWYNo")
	@ResponseBody
	public JsonResult getYWYNo(Model model, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult(false);
		String userId = WebValueUtil.getWebValue(request, "userId");
		User user = userDao.getOne(new BigInteger(userId));
		jsonResult.setFlag(true);
		jsonResult.setObj(user.getUserNo());
		return jsonResult;
	}
}
